import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import i18n from 'i18next'
const initialState = {
  onlineStatus: false,
  lang: 'zh' as Language,
  // 编辑模式
  editMode: 'query' as EditMode
}

export const systemSlice = createSlice({
  name: 'system',
  initialState,
  reducers: {
    // 切换在线状态
    changeOnlineStatus: (state, action: PayloadAction<boolean>) => {
      state.onlineStatus = action.payload
    },
    // 切换语言
    changeLang: (state, action: PayloadAction<Language>) => {
      state.lang = action.payload
      i18n.changeLanguage(action.payload)
    },
    // 切换编辑模式
    changeEditMode(state, action: PayloadAction<EditMode>) {
      state.editMode = action.payload
    }
  }
})

export const { changeOnlineStatus, changeLang, changeEditMode } = systemSlice.actions

const systemReducers = systemSlice.reducer
export default systemReducers
